package com.tom.jmgd.common;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @author kwong
 * 处理http请求基础类
 */
public class HttpClientUtil {
    public static Map doPost(String url, Map<String,String> params, JSONObject jsonData) {
        ////设置超时
        RequestConfig requestconfig = RequestConfig.custom().setConnectionRequestTimeout(10000)
                .setConnectTimeout(10000)
                .setSocketTimeout(60000)
                .build();
        CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(requestconfig)
                .build();
        HttpPost httppost = new HttpPost(url);
        Map<String,Object> rsMap = new HashMap<String,Object>();
        String rs = "";
        int statusCode = 0;
        try {
            ////设置参数
            ////预设参数
            httppost.setHeader("Connection","keep-alive");
            if(params != null) {
                for(Map.Entry<String,String> entry : params.entrySet()) {
                    httppost.setHeader(entry.getKey(),entry.getValue());
                }
            }
            ////添加数据
            if(jsonData != null) {
                StringEntity requestEntity = new StringEntity(jsonData.toJSONString(), StandardCharsets.UTF_8);
                httppost.setEntity(requestEntity);
            }
            ////执行http接口
            CloseableHttpResponse response = httpclient.execute(httppost);
            try {
                HttpEntity entity = response.getEntity();
                if(entity != null) {
                    rs = EntityUtils.toString(entity,"UTF-8");
                }
            }
            catch (Exception e1) {
                rs = e1.getMessage();
                statusCode = -1;
            } finally {
                response.close();
            }
        }
        catch(Exception e) {
            rs = e.getMessage();
            statusCode = -1;
        } finally {
            try {
                if(httpclient != null) {
                    httpclient.close();
                }
            } catch (IOException e2) {
                rs = e2.getMessage();
                statusCode = -1;
            }
        }
        rsMap.put("statusCode",statusCode);
        if(statusCode == -1) {
            rsMap.put("msg",rs);
            rsMap.put("data",null);
        }
        else {
            rsMap.put("msg","success");
            rsMap.put("data",rs);
        }
        return rsMap;
    }
    public static Map doGet(String url) {
        ////设置超时
        RequestConfig requestconfig = RequestConfig.custom().setConnectionRequestTimeout(10000)
                .setConnectTimeout(10000)
                .setSocketTimeout(60000)
                .build();
        CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(requestconfig)
                .build();
        HttpGet httpget = new HttpGet(url);
        Map<String,Object> rsMap = new HashMap<String,Object>();
        String rs = "";
        int statusCode = 0;
        try {
            ////设置参数
            ////预设参数
            httpget.setHeader("Connection","keep-alive");
            httpget.setHeader("Content-Type","application/json;charset=UTF-8");
            ////执行http接口
            CloseableHttpResponse response = httpclient.execute(httpget);
            try {
                HttpEntity entity = response.getEntity();
                if(entity != null) {
                    rs = EntityUtils.toString(entity,"UTF-8");
                }
            }
            catch (Exception e1) {
                rs = e1.getMessage();
                statusCode = -1;
            } finally {
                response.close();
            }
        }
        catch(Exception e) {
            rs = e.getMessage();
            statusCode = -1;
        } finally {
            try {
                if(httpclient != null) {
                    httpclient.close();
                }
            } catch (IOException e2) {
                rs = e2.getMessage();
                statusCode = -1;
            }
        }
        rsMap.put("statusCode",statusCode);
        if(statusCode == -1) {
            rsMap.put("msg",rs);
            rsMap.put("data",null);
        }
        else {
            rsMap.put("msg","success");
            rsMap.put("data",rs);
        }
        return rsMap;
    }
    public static Map doPut(String url, Map<String,String> params, JSONObject jsonData) {
        ////设置超时
        RequestConfig requestconfig = RequestConfig.custom().setConnectionRequestTimeout(10000)
                .setConnectTimeout(10000)
                .setSocketTimeout(60000)
                .build();
        CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(requestconfig)
                .build();
        HttpPut httpput = new HttpPut(url);
        Map<String,Object> rsMap = new HashMap<String,Object>();
        String rs = "";
        int statusCode = 0;
        try {
            ////设置参数
            ////预设参数
            httpput.setHeader("Connection","keep-alive");
            if(params != null) {
                for(Map.Entry<String,String> entry : params.entrySet()) {
                    httpput.setHeader(entry.getKey(),entry.getValue());
                }
            }
            ////添加数据
            if(jsonData != null) {
                StringEntity requestEntity = new StringEntity(jsonData.toString(), StandardCharsets.UTF_8);
                httpput.setEntity(requestEntity);
            }
            ////执行http接口
            CloseableHttpResponse response = httpclient.execute(httpput);
            try {
                HttpEntity entity = response.getEntity();

                if(entity != null) {
                    rs = EntityUtils.toString(entity,"UTF-8");
                }
            }
            catch (Exception e1) {
                rs = e1.getMessage();
                statusCode = -1;
            } finally {
                response.close();
            }
        }
        catch(Exception e) {
            rs = e.getMessage();
            statusCode = -1;
        } finally {
            try {
                if(httpclient != null) {
                    httpclient.close();
                }
            } catch (IOException e2) {
                rs = e2.getMessage();
                statusCode = -1;
            }
        }
        rsMap.put("statusCode",statusCode);
        if(statusCode == -1) {
            rsMap.put("msg",rs);
            rsMap.put("data",null);
        }
        else {
            rsMap.put("msg","success");
            rsMap.put("data",rs);
        }
        return rsMap;
    }
}
